package com.guodi.model.controller;

import com.guodi.common.utils.AjaxResult;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.model.service.INatResourcesService;
import com.guodi.model.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.*;

/**
 * @描述：Excel解析控制类
 * @作者: lzy
 * @日期: 2021-03-18
 */
@RestController
@RequestMapping("/NatResources")
@Api(value = "API - NatResourcesController", description = "Excel解析控制类")
public class NatResourcesController extends BaseController {

    @Resource
    private INatResourcesService natResourcesService;

    /**
     * @作者: 林楷 2021/12/8
     * @描述: 解析excel转指定格式
     * @参数:
     * @返回:
     */
    @PostMapping("/ImportData")
    @ApiOperation(value = "根据模板导入数据")
    public AjaxResult importData(MultipartFile file) throws Exception {
        return natResourcesService.analysisToMap(file);
    }

    /**
     * @描述:根据模板导入数据
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2021/3/18 16:55
     **/
//    @PostMapping("/ImportData")
//    @ApiOperation(value = " 根据模板导入数据")
//    public AjaxResult ImportData(MultipartFile file) throws Exception {
//        List<Map<String, String>> list = fileToList(file);
//        return new AjaxResult(list);
//    }

    /**
     * @描述:Excel文件转list
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2021/3/18 16:56
     **/
    private List<Map<String, String>> fileToList(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        Workbook wb = WorkbookFactory.create(inputStream);
        inputStream.close();
        Sheet sheet = wb.getSheetAt(0);
        //最后一行数据
        int rowEnd = sheet.getLastRowNum();
        //从第二行开始
        int rowStart = 1;
        //获取合并单元格的集合
        List<CellRangeAddress> listCombineCell = ExcelUtil.getCombineCell(sheet);
        //封装excel数据到list中
        List<Map<String, String>> list = new ArrayList<>();
        Row titleRow = sheet.getRow(0);
        //获取内容信息
        for (int i = rowStart; i <= rowEnd; ++i) {
            Row currentRow = sheet.getRow(i);
            if (Objects.isNull(currentRow)) {
                continue;
            }
            Map<String, String> map = new LinkedHashMap<>();
            //将单元格的值放入pd中，key为单元格的索引，value为单元格的值
            int flag = 0;
            for (int j = 0; j < currentRow.getLastCellNum(); ++j) {
                Cell title = titleRow.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                String titleValue = title.getStringCellValue();
                //将null转化为Blank
                Cell data = currentRow.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                data.setCellType(CellType.STRING);
                String cellValue = ExcelUtil.getCellValue(listCombineCell, data, sheet);
                if (flag == 0 && StringUtils.isNotBlank(cellValue)) {
                    flag = 1;
                }
                map.put(titleValue, cellValue);
            }
            if (flag == 1) {
                list.add(map);
            }
        }
        return list;
    }
}